package REU.src;
import java.util.LinkedList;



public class SigList {
	public final int ROWS = 26;
    private LinkedList<Entry>[] entries;
    private final int UPPER_ROOT = 65;
    private final int LOWER_ROOT = 97;
        
        
    @SuppressWarnings("unchecked")
	public SigList(){
    	entries = (LinkedList<Entry>[])new LinkedList[ROWS]; 
    	
    }
        
    public boolean add(String fname, String mname, String lname){
    	//Create new entry
        Entry new_entry = new Entry(fname, mname, lname);
        //Determine what row to reference in the list
        char ch = lname.charAt(0);
        int entries_index = ch - UPPER_ROOT;
        if(Character.isLowerCase(ch)){
        	entries_index = ch - LOWER_ROOT;
        }
        //Get the number of entries in the specified row
        int numb_of_entries = entries[entries_index].size();
        //Compare new entry with current entries in list
        for(int i = 0; i < numb_of_entries; i++){
        	//if entry is an exact match then return false
        	if((entry_comparison(entries[entries_index].get(i), new_entry)) == 0 ){
        		return false;
        		
        	}
        	//if entry is greater than new entry, insert new entry at that index
            if((entry_comparison(entries[entries_index].get(i), new_entry)) > 0 ){
            	entries[entries_index].add(i, new_entry);
            	break;
            	
            }
            //if last entry is less than new entry, add new entry to end of list
            if(((numb_of_entries - 1) == i) && ((entry_comparison(entries[entries_index].get(i), new_entry))< 0)){
            	entries[entries_index].addLast(new_entry);
            	break;
                	
            }
        	
        }
        	
        return true;
        
    }
        
    public boolean remove(String fname, String mname, String lname){
    	//Create entry to search for
        Entry entryToRemove = new Entry(fname, mname, lname);
    	//Determine what row to reference in the list
        int entries_index = lname.charAt(0) - ROOT;
        //Search for matching entry
        if(search_entry(entryToRemove)>= 0 ){
        	//Matching entry found, remove
        	entries[entries_index].remove(search_entry(entryToRemove));
        	return true;
        }
        //Entry not found
        else
        	return false;
           
    }
    
    public boolean edit(String fname, String mname, String lname, int fieldIndex, String updatedInfo){
    	//Create new entry
        Entry entryToEdit = new Entry(fname, mname, lname);
        //Determine what row to reference in the list
        int entries_index = lname.charAt(0) - ROOT;
        //Search for matching entry
        if(search_entry(entryToEdit)>= 0 ){
        	if(fieldIndex == 0){
        		entries[entries_index].get(search_entry(entryToEdit)).setLastName(updatedInfo);
        		return true;
        	}
        	
        	if(fieldIndex == 1){
        		entries[entries_index].get(search_entry(entryToEdit)).setFirstName(updatedInfo);
        		return true;
        	}
        	
        	if(fieldIndex == 2){
        		entries[entries_index].get(search_entry(entryToEdit)).setMidName(updatedInfo);
        		return true;
        	}
        	
        }
        
        return false;
    	
    }
        
    public int entry_comparison(Entry a, Entry b){
    	String a_name = a.getLastName();
    	String b_name = b.getLastName();
    	int comp_val = a_name.compareToIgnoreCase(b_name);
    	if(comp_val == 0){
            	 
    		//last names are the same so compare first names
    		a_name = a.getFirstName();
    		b_name = b.getLastName();
    		comp_val = a_name.compareToIgnoreCase(b_name);
                 
    		if(comp_val == 0){
                	 
    			//first names are the same so compare middle names
    			a_name = a.getMidName();
    			b_name = b.getMidName();
    			comp_val = a_name.compareToIgnoreCase(b_name);
                	 
    		}
    	}
    	return comp_val;
        	
    }
    
    public int search_entry(Entry entryToFind){
    	//Get last name of entry we are searching for
    	String entryToFind_lname = entryToFind.getLastName();
    	//Determine what row to reference in the list
        int entries_index = entryToFind_lname.charAt(0) - ROOT;
        //Get the number of entries in the specified row
        int numb_of_entries = entries[entries_index].size();
    	for(int i = 0; i < numb_of_entries; i++){
        	//If entry is an exact match then return entry index
        	if((entry_comparison(entries[entries_index].get(i), entryToFind)) == 0 ){
        		return i;
        		
        	}
    	}
    	//If exact match not found return -1
    	return -1;
    	
    }
        
  //  public static void main (String[] args){
    //	SigList example = new SigList();
    	//example.add("Vincent", "Maurice", "Sanders");
        	
        	
        	
        	
        	
    //}
}